package com.tgpms.web.earlierStage.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.util.Common;
import com.tgpms.web.earlierStage.dao.TaskDefinitionMapper;
import com.tgpms.web.earlierStage.dao.TaskJournalMapper;
import com.tgpms.web.earlierStage.entity.TaskDefinition;
import com.tgpms.web.earlierStage.entity.TaskJournal;
import com.tgpms.web.earlierStage.service.EarlierTaskJouService;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
public class EarlierTaskJouServiceImpl implements EarlierTaskJouService {
	
	@Autowired
	private TaskJournalMapper taskJournalMapper;
	@Autowired
	private TaskDefinitionMapper taskDefinitionMapper;
	
	@Override
	public void countPercents() {
		List<TaskJournal> list = taskJournalMapper.getJournalByids();
		for(TaskJournal task : list) {
			countPercent(task,0);
		}
	}
	
	//新增或者修改日志进度百分比汇总计算上级任务进度百分比
	@Override
	public synchronized void countPercent(TaskJournal taskJou,int i) {
		String id = taskJou.getId();
		if(i==1) {
			taskJou = taskJournalMapper.selectByPrimaryKey(id, taskJou.getJournalId());
		}else if(i==2) {
			taskJournalMapper.deleteByid(id);
			TaskDefinition task = taskDefinitionMapper.selectByPrimaryKey(id);
			taskJou.setProjectId(task.getProjectId());
			taskJou.setJournalDesc("汇总");
			taskJou.setPersonLiable("无");
			taskJou.setDispute("N");
			taskJou.setStauts("0");
			taskJou.setInsertUser("汇总人");
		}
		String parentid = "";
		while(true) {
			//查询父级id
			TaskDefinition taskDefinition = taskDefinitionMapper.selectByPrimaryKey(id);
			if(taskDefinition==null) {
				break;
			}
			parentid = taskDefinition.getParentId();
			if(parentid.equals("0")) {
				break;
			}else {
				id = parentid;
				String Ratios = taskJournalMapper.getParentRatios(parentid);
				int yRatios = taskJournalMapper.getParentYRatios(parentid);
				BigDecimal decimal = new BigDecimal(Ratios);
				if(yRatios>0) {
					decimal = decimal.add(new BigDecimal(yRatios+""));
				}
				int count = taskJournalMapper.getParentCounts(parentid);
				decimal = decimal.divide(new BigDecimal(count+""),2,BigDecimal.ROUND_UP);
				System.out.println("**************&&&&&&&#################计算的百分比为："+decimal);
				taskJournalMapper.deleteByid(id);
				taskJou.setRatio(decimal);
				taskJou.setId(id);
				taskJou.setJournalId(Common.fromDateFormats()+UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
				taskJou.setRowstamp(taskJou.getJournalId());
				taskJou.setInsertDate(new Date());
				taskJournalMapper.insertSelective(taskJou);
			}
		}
	}

	@Override
	public Result addTaskJou(TaskJournal taskJou, Result result) {
		//通过唯一键查询重复
//		if(taskJournalMapper.selByUniqueKey(taskJou.getJournalId())!=null) {
//			result.setSuccess(false);
//			result.setMsg(ExceptionExplain.ERROR_BY_INSERT_REPETITION.getExplain());
//			return result;
//		}
		taskJou.setJournalId(Common.fromDateFormats()+UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
		taskJou.setRowstamp(taskJou.getJournalId());
		taskJou.setInsertDate(new Date());
		int i = taskJournalMapper.insertSelective(taskJou);
		if(i==1) {
			result.setMsg(ExceptionExplain.SUCCESS_BY_INSERT.getExplain());
			result.setData(taskJou.getId());
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
		}
		return result;
	}

	@Override
	public Result updateTaskJou(TaskJournal taskJou, Result result) {
		taskJou.setModifyDate(new Date());
		int i = taskJournalMapper.updateByPrimaryKeySelective(taskJou);
		if(i==1) {
			result.setMsg(ExceptionExplain.SUCCESS_BY_UPDATE.getExplain());
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
		}
		return result;
	}

	@Override
	public Result deleteTaskJou(String id,String journalId, Result result) {
		TaskJournal taskJou = taskJournalMapper.selectByPrimaryKey(id, journalId);
		int i = taskJournalMapper.deleteByPrimaryKey(id,journalId);
		if(i==1) {
			new Thread(){
				public void run(){
					countPercent(taskJou,0);
				}
			}.start();
			result.setMsg(ExceptionExplain.SUCCESS_BY_DELETE.getExplain());
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_DELETE.getExplain());
		}
		return result;
	}


}
